package com.base.cn.platform.os.service.qa.record;

import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.qa.record.QaTeacherCommissionDao;
import com.base.cn.platform.os.entity.manage.teacher.TeacherExpand;
import com.base.cn.platform.os.entity.qa.question.QaQuestion;
import com.base.cn.platform.os.entity.qa.question.QuestionCondition;
import com.base.cn.platform.os.entity.qa.record.QaTeacherCommission;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import com.base.cn.platform.os.service.qa.question.QaQuestionBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 问答讲师分成记录Biz
 * @author LIZHIPENG
 * @since 2018/5/2
 */
@Service
public class QaTeacherCommissionBiz extends QaRecordBaseBiz<QaTeacherCommission, QaTeacherCommissionDao> {

    @Autowired
    private QaQuestionBiz qaQuestionBiz;
    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    QaTeacherCommissionDao QaTeacherCommissionDao;

    /**
     * 查询数量
     * @param commission 条件对象
     * @return 数量
     */
    public BigDecimal getCount(QaTeacherCommission commission){
        String whereSql = this.getWhereSql(commission);
        return this.count(whereSql);
    }

    /**
     * 添加讲师分成记录
     * @param commissionList
     */
    public void addTeacherCommission(List<QaTeacherCommission> commissionList){
        this.batchSave(commissionList);
    }

    /**
     * 查询问答讲师分成记录表（分页）
     * @param page 分页
     * @param teacherCommission 条件
     * @param questionData 是否设置问题数据：true：是，false：否
     * @param userData 是否设置用户数据：true：是，false：否
     * @param teacherData 是否设置收益讲师数据：true：是，false：否
     * @return
     */
    public PageInfo<QaTeacherCommission> queryQaTeacherCommissionListPage(Pagination page, QaTeacherCommission teacherCommission,
                                                                   boolean questionData, boolean userData, boolean teacherData){
        String whereSql = getWhereSql(teacherCommission);
        PageInfo<QaTeacherCommission> teacherCommissionPageInfo = this.findPage(whereSql, page, null);
        teacherCommissionPageInfo.setList(this.setExtendData(teacherCommissionPageInfo.getList(), questionData, userData, teacherData));
        return teacherCommissionPageInfo;
    }

    /**
     * 查询问答讲师分成记录表（分页）
     * @param teacherCommission 条件
     * @param questionData 是否设置问题数据：true：是，false：否
     * @param userData 是否设置用户数据：true：是，false：否
     * @param teacherData 是否设置收益讲师数据：true：是，false：否
     * @return List<QaTeacherCommission>
     */
    public List<QaTeacherCommission> queryQaTeacherCommissionList(QaTeacherCommission teacherCommission,boolean questionData, boolean userData, boolean teacherData){
        String whereSql = getWhereSql(teacherCommission);
        List<QaTeacherCommission> qaTeacherCommissionList = this.find(whereSql, null, null);
        return this.setExtendData(qaTeacherCommissionList, questionData, userData, teacherData);
    }


    /**
     * 查询问答讲师分成数
     * @return
     */
    public List<TeacherExpand> qaTeacherCommissionSum(BigDecimal teacherId){

        return  QaTeacherCommissionDao.qaTeacherCommissionSum(QaTeacherCommission.class,teacherId);
    }

    //===============================================================

    /**
     * 设置扩展数据
     * @param teacherCommissionList 讲师分成收益列表
     * @param questionData 是否设置问题数据：true：是，false：否
     * @param userData 是否设置用户数据：true：是，false：否
     * @param teacherData 是否设置收益讲师数据：true：是，false：否
     * @return
     */
    private List<QaTeacherCommission> setExtendData(List<QaTeacherCommission> teacherCommissionList, boolean questionData, boolean userData, boolean teacherData){
        if(questionData){
            teacherCommissionList = setQuestionData(teacherCommissionList, questionData);
        }
        if(userData){
            teacherCommissionList = setUserData(teacherCommissionList, userData);
        }if(teacherData){
            teacherCommissionList = setTeacherData(teacherCommissionList, teacherData);
        }
        return teacherCommissionList;
    }

    /**
     * 设置问题数据
     * @param teacherCommissionList  讲师分成收益列表
     * @param questionData 是否设置问题数据：true：是，false：否
     * @return
     */
    private List<QaTeacherCommission> setQuestionData(List<QaTeacherCommission> teacherCommissionList, boolean questionData){
        if(ObjectUtils.isNotEmpty(teacherCommissionList) && questionData){
            String questionIds = teacherCommissionList.stream().map(e -> e.getQuestionId().toString()).distinct().collect(Collectors.joining(","));
            QuestionCondition condition = new QuestionCondition();
            condition.setIds(questionIds);
            condition.setNoStatusLimit(true);
            List<QaQuestion> questionList = qaQuestionBiz.findQuestionList(condition, false, false, false, false, false, false, false);
            if(ObjectUtils.isNotEmpty(questionList)){
                Map<BigDecimal, QaQuestion> questionMap = questionList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
                for (QaTeacherCommission teacherCommission : teacherCommissionList) {
                    QaQuestion question = questionMap.get(teacherCommission.getQuestionId());
                    if(ObjectUtils.isNotEmpty(question)){
                        teacherCommission.setQaQuestion(question);
                    }
                }
            }
        }
        return teacherCommissionList;
    }

    /**
     * 设置用户数据
     * @param teacherCommissionList 讲师分成收益列表
     * @param userData 是否设置用户数据：true：是，false：否
     * @return
     */
    private List<QaTeacherCommission> setUserData(List<QaTeacherCommission> teacherCommissionList, boolean userData){
        if(ObjectUtils.isNotEmpty(teacherCommissionList) && userData){
            String userIds = teacherCommissionList.stream().map(e -> e.getUserId().toString()).distinct().collect(Collectors.joining(","));
            Map<BigDecimal, Map<String,Object>> userMap = cusUserService.queryCusUserMapByUserIds(userIds, false, false, false, false, false, false, false);
            if(ObjectUtils.isNotEmpty(userMap)){
                for (QaTeacherCommission teacherCommission : teacherCommissionList) {
                    teacherCommission.setUserMap((Map<String, Object>)userMap.get(teacherCommission.getUserId()));
                }
            }
        }
        return teacherCommissionList;
    }

    /**
     * 设置讲师数据
     * @param teacherCommissionList 讲师分成收益列表
     * @param teacherData 是否设置收益讲师数据：true：是，false：否
     * @return
     */
    private List<QaTeacherCommission> setTeacherData(List<QaTeacherCommission> teacherCommissionList, boolean teacherData){
        if(ObjectUtils.isNotEmpty(teacherCommissionList) && teacherData){
            String teacherIds = teacherCommissionList.stream().map(e -> e.getTeacherId().toString()).distinct().collect(Collectors.joining(","));
            Map<BigDecimal, Map<String, Object>> teacherMap = sysTeacherService.findSysTeacherToMapBySimpleIds(teacherIds);
            if(ObjectUtils.isNotEmpty(teacherMap)){
                for (QaTeacherCommission teacherCommission : teacherCommissionList) {
                    teacherCommission.setTeacherMap(teacherMap.get(teacherCommission.getTeacherId()));
                }
            }
        }
        return teacherCommissionList;
    }
}
